package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.RoleResourcesService;
import com.yixun.qfbao.apidto.AgencyRoleAuthResourcesResDto;
import com.yixun.qfbao.apidto.GetAuthorizeRoleListReqDto;
import com.yixun.qfbao.apidto.GetAuthorizeRoleListResDto;
import com.yixun.qfbao.dao.DeptPermissionDao;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.DeptPermission;
import com.yixun.qfbao.model.dto.DeptPermissionDto;
import com.yixun.qfbao.model.enums.BelongsEnum;
import com.yixun.qfbao.vo.GetAuthorizeRoleListReqVo;
import com.yixun.qfbao.xf.model.enums.DeptPermissionEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 获取角色授权资源列表
 * @author guifang.weng
 * @create 2017-11-22 17:03
 **/
@Service("getAuthorizeRoleListHander")
public class GetAuthorizeRoleListHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetAuthorizeRoleListHander.class);

    @Autowired
    private RoleResourcesService roleResourcesService;
    @Autowired
    DeptPermissionDao deptPermissionDao;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<AgencyRoleAuthResourcesResDto> res = new BaseResponseDTO<>();
        GetAuthorizeRoleListReqVo reqVo = (GetAuthorizeRoleListReqVo) reqDto;
        GetAuthorizeRoleListReqDto getAuthorizeRoleListReqDto = reqVo.getParams();
        try {
            AgencyRoleAuthResourcesResDto resDto = roleResourcesService.getAgencyRoleAuthResources(getAuthorizeRoleListReqDto);

            // 默认个人
            resDto.setDeptPermission(DeptPermissionEnum.MYSELF.getNo());
            DeptPermissionDto dpDto = new DeptPermissionDto();
            dpDto.setBelongs(BelongsEnum.ROLE.getNo());
            dpDto.setBelongsId(getAuthorizeRoleListReqDto.getRoleId());
            List<DeptPermission> deptPermissionList = deptPermissionDao.selectListByDto(dpDto);
            if(CollectionUtils.isNotEmpty(deptPermissionList)){
                resDto.setDeptPermission(deptPermissionList.get(0).getDeptPermission());
            }

            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("GetAuthorizeRoleListHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取角色授权资源列表失败");
        }
        return res;
    }
}
